package daohibernate;

import hibernate.HibernateUtil;

import java.util.List;

import negocio.beans.central.Proveedor;

import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class ProveedorDAO {
	private static ProveedorDAO instancia = null;
	private static SessionFactory sf = null;	
	
	public static ProveedorDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ProveedorDAO();
		} 
		return instancia;
	}
	
	public void altaProveedor(Proveedor p){
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.persist(p);
		
		s.flush();
		
		s.getTransaction().commit();
		s.close();
	}
	
	public void modificacionProveedor(Proveedor p){
		Session s = sf.openSession();
		s.beginTransaction();
				
		s.update(p);
		
		s.flush();
		
		s.getTransaction().commit();
		s.close();
	}
	
	public void bajaProveedor(Proveedor p){		
		Session s = sf.openSession();
		s.beginTransaction();
		
		
		s.delete(p);
		
		s.flush();
		
		s.getTransaction().commit();
		s.close();
	}
	
	public List<Proveedor> obtenerProveedores(){
		Session s = sf.openSession();		
		@SuppressWarnings("unchecked")
		List<Proveedor> lista = s.createQuery("from Proveedor").list();
		s.close();
		return lista;
	}
	
	public Proveedor obtenerProveedor(String razonSocial){
		Session s = sf.openSession();

		@SuppressWarnings("unchecked")
		List<Proveedor> proveedores =  s.createQuery("from Proveedor where razonSocial = '" + razonSocial + "'").list();
		s.close();
		
		for(Proveedor p : proveedores){
			return p;
		}
		
		return null;
		
	}

	
}
